<?php
namespace app\admin\command\statistics;

use app\admin\model\user\User;
use app\admin\model\user\Order;
use app\admin\model\agent\Agent;
use app\admin\model\tongji\StatisticsDay;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class UserStatistics extends Command{
    protected function configure()
    {
        $this->setName('day:bouns')->setDescription('每日瓜分详情统计');
    }
    protected function execute(Input $input, Output $output)
    {
        $user_model=new User();
        $order_model=new Order();
        $agent_model=new Agent();
        $statictics_day_model=new StatisticsDay();
        $user_num=$user_model->whereTime('create_time','yesterday')->count();
        $ip_num=$user_model->whereTime('create_time','yesterday')->group('ip')->count();
        $order_user_num=$order_model->whereTime('create_time','yesterday')->where('status',1)->group('user_id')->count();
        $order_price=$order_model->whereTime('create_time','yesterday')->where('status',1)->sum('price');
        $old_date=$statictics_day_model->where('date',date('Ymd',strtotime('-1 day')))->find();
        $agent_list=$agent_model->select()->toArray();

        //统计每个代理数据
        foreach ($agent_list as $k=>$v){
            $user_num_agent=$user_model->whereTime('create_time','yesterday')->where('agent_id',$v['id'])->count();
            $ip_num_agent=$user_model->whereTime('create_time','yesterday')->where('agent_id',$v['id'])->group('ip')->count();
            $order_user_num_agent=$order_model->whereTime('create_time','yesterday')->where('status',1)->where('agent_id',$v['id'])->group('user_id')->count();
            $order_price_agent=$order_model->whereTime('create_time','yesterday')->where('status',1)->where('agent_id',$v['id'])->sum('price');
            $data[]=[
                'date'=>date('Ymd',strtotime('-1 day')),
                'user_count'=>$user_num_agent,
                'ip_num'=>$ip_num_agent,
                'hy_user_num'=>0,
                'rechage_num'=>$order_user_num_agent,
                'rechage_price'=>$order_price_agent,
                'agent_id'=>$v['id'],
                'p_agent_id'=>$v['p_agent_id'],
            ];
        }
        $statictics_day_model->saveAll($data);
        //记录总数据
        if(!empty($old_date)){
            $old_date->user_count=$user_num;
            $old_date->ip_num=$ip_num;
            $old_date->hy_user_num=0;
            $old_date->rechage_num=$order_user_num;
            $old_date->rechage_price=$order_price;
            $old_date->save();
        }else{
            $data=[
                'date'=>date('Ymd',strtotime('-1 day')),
                'user_count'=>$user_num,
                'ip_num'=>$ip_num,
                'hy_user_num'=>0,
                'rechage_num'=>$order_user_num,
                'rechage_price'=>$order_price,
            ];
            $statictics_day_model->save($data);
        }
    }
}
